using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using T.FAS.Runtime.Base.Common;
using T.FAS.Runtime.Base.Dao;
using T.FAS.Runtime.Base.Entity;

namespace T.FAS.Runtime.Base.Service
{
    public class ChartService
    {
        readonly ChartLibDao chartLibDao = new ChartLibDao();
        readonly ChartInstanceDao instanceDao = new ChartInstanceDao();
        readonly ChartInstanceApplyDao applyDao = new ChartInstanceApplyDao();
        /// <summary>
        /// 查询图表库
        /// </summary>
        /// <returns></returns>
        public List<ChartLib> GetChartLibs()
        {
            return chartLibDao.GetChartLibs();
        }

        /// <summary>
        /// 查询图表列表
        /// </summary>
        /// <returns></returns>
        public List<ChartInstanceApply> GetChartInstanceApplys(TargetType targetType, string targetID)
        {
            List<ChartInstanceApply> instanceApplies = applyDao.GetChartInstanceApplys(targetType, targetID);
            if (instanceApplies.Count > 0)
            {
                List<ChartLib> chartLibs = chartLibDao.GetChartLibs();
                foreach (var item in instanceApplies)
                {
                    item.ChartLib = chartLibs.FirstOrDefault(t => t.ID == item.ChartLibItemID);
                }
            }

            return instanceApplies;
        }

        /// <summary>
        /// 创建或更新
        /// </summary>
        /// <param name = "chart"></param>
        public Dictionary<string, string> CreateOrUpdateChart(ChartInstanceApply chart)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            string UserName = UserUtil.GetCurrentUserName();
            chart.LastModifyTime = DateTime.Now;
            chart.LastModifier = UserName;
            try
            {
                using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted }))
                {
                    chart.ID = Guid.NewGuid().ToString();
                    var instance = new ChartInstance { LastModifier = chart.LastModifier, LastModifyTime = chart.LastModifyTime, Code = chart.Code, Name = chart.Name, ApplyID = chart.ID, ChartLibItemID = chart.ChartLibItemID, DataSourceID = chart.DataSourceID, DemoData = chart.DemoData, ChartContent = chart.ChartContent, };
                    if (string.IsNullOrEmpty(chart.ID))
                    {
                        instance.ID = Guid.NewGuid().ToString();
                        instanceDao.Add(new List<object> { instance });
                        chart.ChartInstanceID = instance.ID;
                        applyDao.Add(new List<object> { chart });
                    }
                    else
                    {
                        instance.ID = chart.ChartInstanceID;
                        instanceDao.Update(new List<object> { instance });
                        applyDao.Update(new List<object> { chart });
                    }

                    scope.Complete();
                }
                dic["state"] = "1";
                dic["message"] = $"{T.ServiceComponent.I18N.Service.TermService.Current.GetText("T-Standard-Common-00000085", "保存成功")}";
                dic["chartID"] = chart.ID;
            }
            catch (Exception e)
            {
                dic["state"] = "0";
                dic["errmsg"] = e.ToString();
            }

            return dic;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name = "id"></param>
        public Dictionary<string, string> DeleteChart(string id)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            try
            {
                using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted }))
                {
                    applyDao.Delete(new List<string> { id });
                    instanceDao.DeleteInstance(id);
                    scope.Complete();
                }
                dic["state"] = "1";
                dic["message"] = $"{T.ServiceComponent.I18N.Service.TermService.Current.GetText("T_FAS_fasr_Base_882904134", "删除成功")}";
            }
            catch (Exception e)
            {
                dic["state"] = "0";
                dic["errmsg"] = e.ToString();
            }
            return dic;
        }
    }
}